/*
 * Copyright 2010 - Jeremie Brebec
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package org.jb.sharedbills.client;

import org.jb.sharedbills.client.gin.ActionType;
import org.jb.sharedbills.client.gin.BillType;
import org.jb.sharedbills.client.gin.GroupType;
import org.jb.sharedbills.client.gin.MemberType;
import org.jb.sharedbills.client.gin.MessageType;
import org.jb.sharedbills.client.resources.Resources;
import org.jb.sharedbills.client.stores.EventLoader;
import org.jb.sharedbills.shared.model.Event;

import com.extjs.gxt.ui.client.Style.LayoutRegion;
import com.extjs.gxt.ui.client.Style.Orientation;
import com.extjs.gxt.ui.client.event.ComponentEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.MessageBoxEvent;
import com.extjs.gxt.ui.client.util.Margins;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.HtmlContainer;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.TabItem;
import com.extjs.gxt.ui.client.widget.TabPanel;
import com.extjs.gxt.ui.client.widget.Viewport;
import com.extjs.gxt.ui.client.widget.button.ToolButton;
import com.extjs.gxt.ui.client.widget.layout.BorderLayout;
import com.extjs.gxt.ui.client.widget.layout.BorderLayoutData;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.layout.RowData;
import com.extjs.gxt.ui.client.widget.layout.RowLayout;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.AbstractImagePrototype;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Widget;
import com.google.inject.Inject;

public class ApplicationViewPort extends Viewport {
	
	@Inject
	public ApplicationViewPort( @MemberType ContentPanel memberPanel,
								@GroupType ContentPanel groupPanel,
								@BillType ContentPanel billPanel,
								@ActionType ContentPanel actionPanel,
								@MessageType ContentPanel messagePanel,
								Widget titlePanel,
								Image savePanel,
								EventLoader eventLoader) {
		
		setLayout(new BorderLayout());
		
	    BorderLayoutData data = new BorderLayoutData(LayoutRegion.NORTH, 33);
	    data.setMargins(new Margins());
	    data.setSplit(true);
	    HtmlContainer titleContainer = new HtmlContainer("<div id='event-saver'></div><div id=event-title></div>");
	    titleContainer.setId("event-header");
	    titleContainer.addStyleName("x-small-editor");
	    add( titleContainer, data);
	    
	    titleContainer.add(titlePanel, "#event-title");
	    titleContainer.add( savePanel , "#event-saver" );

		ContentPanel centerPanel = new ContentPanel();
	    centerPanel.setBorders(false);
	    centerPanel.setHeaderVisible(false);
	    centerPanel.setLayout(new FitLayout());
	    centerPanel.setBodyBorder(false);

	    BorderLayoutData centerPaneldata = new BorderLayoutData(LayoutRegion.CENTER);
	    centerPaneldata.setMargins(new Margins(5, 5, 5, 5));
	    add(centerPanel, centerPaneldata);

		ContentPanel eventView = new ContentPanel();
		eventView.setBorders(false);
		eventView.setHeaderVisible(false);
		eventView.setLayout( new BorderLayout() );
		eventView.setBodyBorder(false);
		centerPanel.add( eventView );
		
		final ContentPanel west = new ContentPanel();
		west.setHeading("Participants");
		west.setBorders(false);
		west.setBodyBorder(false);
		west.setHeaderVisible(true);
		west.setLayout( new RowLayout(Orientation.VERTICAL) );
		west.setAnimCollapse(true);
		
		LayoutContainer westTopContainer = new LayoutContainer( new BorderLayout());
		
		
		TabItem members = new TabItem( "Personnes");
		members.setIcon( AbstractImagePrototype.create(Resources.INSTANCE.user()) );
		members.setLayout( new FitLayout() );
		members.setBorders(false);
		members.add( memberPanel );
			
		TabItem groups = new TabItem("Groupes");
		groups.setIcon( AbstractImagePrototype.create(Resources.INSTANCE.users()) );
		groups.setLayout( new FitLayout() );
		groups.setBorders(false);
		groups.add( groupPanel );
		
		TabPanel tabPanel = new TabPanel();
		tabPanel.setBodyBorder(false);
		tabPanel.setBorders(false);
		tabPanel.add(members);
		tabPanel.add(groups);

		BorderLayoutData memberLayoutData = new BorderLayoutData(LayoutRegion.CENTER);
		memberLayoutData.setSplit(true);
		westTopContainer.add( tabPanel, memberLayoutData );
		
		BorderLayoutData messageLayoutData = new BorderLayoutData(LayoutRegion.SOUTH);
		messageLayoutData.setCollapsible(true);
		messageLayoutData.setSplit(true);
		messageLayoutData.setFloatable(false);
		messageLayoutData.setMargins( new Margins(5,0,0,0));
		westTopContainer.add( messagePanel, messageLayoutData);
		
		west.add( westTopContainer, new RowData(1, 1) );
		west.add( actionPanel, new RowData(1,-1,new Margins(5,0,0,0) ) );
		
		BorderLayoutData northData = new BorderLayoutData(LayoutRegion.WEST, 350);  
	    northData.setMargins(new Margins(0, 5, 0, 0));  
	    northData.setSplit(true);
	    northData.setCollapsible(true);
	    
	    BorderLayoutData centerData = new BorderLayoutData(LayoutRegion.CENTER);
	    
	    ToolButton collapseBtn = new ToolButton("x-tool-maximize");
	    collapseBtn.addListener(Events.Select, new Listener<ComponentEvent>() {
	    	public void handleEvent(ComponentEvent ce) {
	    		ce.stopEvent();
	    		
	    		west.setVisible( !west.isVisible() );
	    	}
	    });
	    
	    billPanel.getHeader().addTool( collapseBtn );
	
	    eventView.add( billPanel, centerData );
	    eventView.add( west, northData );
	    
	    final MessageBox msgBox = MessageBox.wait("Evenement", "Chargement en cours...", "");
	    msgBox.show();
	    eventLoader.load( new AsyncCallback<Event>() {
			
			@Override
			public void onSuccess(Event result) {
				msgBox.close();
			}
			
			@Override
			public void onFailure(Throwable caught) {
				MessageBox error = MessageBox.alert("Erreur", "Evenement non trouvé", new Listener<MessageBoxEvent>() {
					@Override
					public void handleEvent(MessageBoxEvent be) {}
				});
				msgBox.close();
				error.show();
				error.getDialog().getButtonBar().removeAll();
			}
		});
	}
	
}
